<?php

namespace common\models\db\tables;

use Yii;

/**
 * This is the model class for table "{{%equipment_app_version_update_log}}".
 *
 * @property int $id
 * @property int $equipment_id 设备ID
 * @property int $old_version_id 老app版本ID
 * @property int $new_version_id 新app版本ID
 * @property int $happen_from 发生更新源
 * @property int $happen_at 发生更新时间
 *
 * @property EquipmentAppVersion $newVersion
 * @property Equipment $equipment
 * @property EquipmentAppVersion $oldVersion
 */
class EquipmentAppVersionUpdateLog extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%equipment_app_version_update_log}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['equipment_id', 'old_version_id', 'new_version_id', 'happen_from', 'happen_at'], 'required'],
            [['equipment_id', 'old_version_id', 'new_version_id', 'happen_from', 'happen_at'], 'integer'],
            [['equipment_id', 'old_version_id', 'new_version_id', 'happen_from'], 'unique', 'targetAttribute' => ['equipment_id', 'old_version_id', 'new_version_id', 'happen_from']],
            [['new_version_id'], 'exist', 'skipOnError' => true, 'targetClass' => EquipmentAppVersion::className(), 'targetAttribute' => ['new_version_id' => 'id']],
            [['equipment_id'], 'exist', 'skipOnError' => true, 'targetClass' => Equipment::className(), 'targetAttribute' => ['equipment_id' => 'id']],
            [['old_version_id'], 'exist', 'skipOnError' => true, 'targetClass' => EquipmentAppVersion::className(), 'targetAttribute' => ['old_version_id' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'equipment_id' => Yii::t('app', '设备ID'),
            'old_version_id' => Yii::t('app', '老app版本ID'),
            'new_version_id' => Yii::t('app', '新app版本ID'),
            'happen_from' => Yii::t('app', '发生更新源'),
            'happen_at' => Yii::t('app', '发生更新时间'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getNewVersion()
    {
        return $this->hasOne(EquipmentAppVersion::className(), ['id' => 'new_version_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEquipment()
    {
        return $this->hasOne(Equipment::className(), ['id' => 'equipment_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOldVersion()
    {
        return $this->hasOne(EquipmentAppVersion::className(), ['id' => 'old_version_id']);
    }
}
